package com.zhudeshuai.zhudemolist.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.zhu.zhuCore.utils.BitmapUtils;
import com.zhu.zhuCore.utils.SelectPhotoUtil;
import com.zhu.zhuCore.utils.ToastUtil;
import com.zhudeshuai.zhudemolist.R;


/**
 * 图片处理demo
 *
 * @author zhudeshuai
 * @since 16/4/18
 */
public class PhotoDealActivity extends Activity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {


    private static final int MAX_VALUE = 255;
    private static final int MID_VALUE = 127;

    private ImageView mImageIv;
    private SeekBar mHueSeekBar;
    private SeekBar mSatSeekBar;
    private SeekBar mLumSeekBar;

    private float mHueValue = MID_VALUE;
    private float mSatValue = MID_VALUE;
    private float mLumValue = MID_VALUE;


    private Bitmap mBitmap;
    private Bitmap mResultBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_deal);

        findView();
        initValue();
    }

    private void initValue() {
        mHueSeekBar.setMax(MAX_VALUE);
        mSatSeekBar.setMax(MAX_VALUE);
        mLumSeekBar.setMax(MAX_VALUE);

        mHueSeekBar.setProgress(MID_VALUE);
        mSatSeekBar.setProgress(MID_VALUE);
        mLumSeekBar.setProgress(MID_VALUE);


        mHueSeekBar.setEnabled(false);
        mSatSeekBar.setEnabled(false);
        mLumSeekBar.setEnabled(false);
    }

    private void findView() {
        findViewById(R.id.selectPhoto).setOnClickListener(this);
        findViewById(R.id.savePhoto).setOnClickListener(this);

        mImageIv = (ImageView) findViewById(R.id.image);
        mHueSeekBar = (SeekBar) findViewById(R.id.hueSeekBar);
        mSatSeekBar = (SeekBar) findViewById(R.id.satSeekBar);
        mLumSeekBar = (SeekBar) findViewById(R.id.lumSeekBar);

        mHueSeekBar.setOnSeekBarChangeListener(this);
        mSatSeekBar.setOnSeekBarChangeListener(this);
        mLumSeekBar.setOnSeekBarChangeListener(this);
    }


    public void oldPhoto(View view) {
        if (mBitmap == null)
            return;
        mImageIv.setImageBitmap(BitmapUtils.imageStyleHandle(mBitmap, BitmapUtils.OLD_PHOTO_EFFECT));

    }

    public void negative(View view) {
        if (mBitmap == null)
            return;
        mImageIv.setImageBitmap(BitmapUtils.imageStyleHandle(mBitmap, BitmapUtils.NEGATIVE_EFFECT));
    }

    public void relief(View view) {
        if (mBitmap == null)
            return;
        mImageIv.setImageBitmap(BitmapUtils.imageStyleHandle(mBitmap, BitmapUtils.RELIEF_EFFECT));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.selectPhoto:
                SelectPhotoUtil.getInstance().showPictureSelected(this, new SelectPhotoUtil.IPhotoSelected() {
                    @Override
                    public void onPhotoSelected(Uri uri) {
                        mBitmap = BitmapUtils.maxWHBitmap(uri, 1280);
                        mImageIv.setImageBitmap(mBitmap);
                        mHueSeekBar.setEnabled(true);
                        mSatSeekBar.setEnabled(true);
                        mLumSeekBar.setEnabled(true);
                    }
                });
                break;
            case R.id.savePhoto:
                if (mResultBitmap != null) {
                    String fileName = "PhotoDeal" + System.currentTimeMillis() + ".jpg";
                    BitmapUtils.saveBitmap(mResultBitmap, Environment.getExternalStorageDirectory().getAbsolutePath(), fileName);
                    ToastUtil.show("保存图片成功，" + Environment.getExternalStorageDirectory().getAbsolutePath() + fileName);
                }
                break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        //初始化，没有选择图片
        if (mBitmap == null)
            return;
        switch (seekBar.getId()) {
            case R.id.hueSeekBar:
                mHueValue = (progress - MID_VALUE) * 1.0F / MID_VALUE * 180;
                break;
            case R.id.satSeekBar:
                mSatValue = progress * 1.0F / MID_VALUE;
                break;
            case R.id.lumSeekBar:
                mLumValue = progress * 1.0F / MID_VALUE;
                break;
        }
        mResultBitmap = BitmapUtils.handleImageEffect(mBitmap, mHueValue, mSatValue, mLumValue);
        mImageIv.setImageBitmap(mResultBitmap);

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBitmap != null && !mBitmap.isRecycled()) {
            mBitmap.recycle();
        }
        if (mResultBitmap != null && !mResultBitmap.isRecycled()) {
            mResultBitmap.recycle();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SelectPhotoUtil.getInstance().onActivityResult(requestCode, resultCode, data);
    }

    public static void open(Activity activity) {
        activity.startActivity(new Intent(activity, PhotoDealActivity.class));
    }
}
